package com.minelx.sequence.common;

import com.minelx.sequence.core.StringScanner;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class Templates {
	private final Set<String> variableNames;

	private final String rawText;

	public Templates(String rawText) {
		this.rawText = rawText;
		variableNames = StringScanner.source(rawText)
				.stream(new VariableNameMatcher())
				.collect(Collectors.toSet());
	}

	public Set<String> variables() {
		return variableNames;
	}

	public String render(Map<String, String> parameters) {
		String result = rawText;
		for (Map.Entry<String, String> keyValues : parameters.entrySet()) {
			result = result.replace("$" + keyValues.getKey() + "$", keyValues.getValue());
		}
		return result;
	}
}
